//
//  AppDelegateNotification.swift

//  BaoKeSwift
//
//  Created by duan on 14/10/2017.
//  Copyright © 2017 geek-duan. All rights reserved.
//

import UIKit
import UserNotifications

// MARK: - 通知相关(ios10)
extension AppDelegate {
    
    // MARK: - 申请通知权限
    func replyPushNotificationAuthorization(Options launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
    }

    // MARK: - 获取设备的token值
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

        let device = NSData(data: deviceToken)
        let deviceTokening = device.description.replacingOccurrences(of:"<", with:"").replacingOccurrences(of:">", with:"").replacingOccurrences(of:" ", with:"")
        print("deviceToken：" + deviceTokening)
    }
}

// MARK: - 通知代理方法 
extension AppDelegate: UNUserNotificationCenterDelegate {
   
    // MARK: - 收到通知（本地和远端:都是在前台)
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        
//        let userInfo = notification.request.content.userInfo
        if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! { //远程通知
            
        }
        completionHandler([.sound, .badge, .alert])
    }
    
    // MARK: - 推送接受反馈 (本地和远端:都是在前台)
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

//        let userInfo = response.notification.request.content.userInfo
        if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! { //远程推送接受
        }
        completionHandler()
    }
}

// MARK: - 通知点击事件
extension AppDelegate {
    
    // MARK: - 点击事件分别处理
    @available(iOS 10.0, *)
    private func actionResponse(response: UNNotificationResponse) {
        /// - 点击默认按钮
        if response.actionIdentifier ==  "com.apple.UNNotificationDefaultActionIdentifier" {
            print("defaultAction")
            // FIXME: - 点击进入我的动态
        }
    }
}

